1 // Fig. 7.8: time6.h 2 // Cascading member function calls. 3 4 // Declaration of class Time. 5 // Member functions defined in time6.cpp 6 #ifndef TIME6_H 7 #define TIME6_H 8 9 class Time { 10 public: 11 Time( int = 0, int = 0, int = 0 ); // default constructor 12 13 // set functions 14 Time &setTime( int, int, int ); // set hour, minute, second 15 Time &setHour( int ); // set hour 16 Time &setMinute( int ); // set minute 17 Time &setSecond( int ); // set second 18 19 // get functions (normally declared const) 20 int getHour() const; // return hour 21 int getMinute() const; // return minute 22 int getSecond() const; // return second 23 24 // print functions (normally declared const) 25 void printMilitary() const; // print military time 26 void printStandard() const; // print standard time 27 private: 28 int hour; // 0 - 23 29 int minute; // 0 - 59 30 int second; // 0 - 59 31 }; 32 33 #endif 34 // Fig. 7.8: time.cpp 35 // Member function definitions for Time class. 36 #include "time6.h" 37 #include 38 39 // Constructor function to initialize private data. 40 // Calls member function setTime to set variables. 41 // Default values are 0 (see class definition). 42 Time::Time( int hr, int min, int sec ) 43 { setTime( hr, min, sec ); } 44 45 // Set the values of hour, minute, and second. 46 Time &Time::setTime( int h, int m, int s ) 47 { 48 setHour( h ); 49 setMinute( m ); 50 setSecond( s ); 51 return *this; // enables cascading 52 } 53 54 // Set the hour value 55 Time &Time::setHour( int h ) 56 { 57 hour = ( h >= 0 && h < 24 ) ? h : 0; 58 59 return *this; // enables cascading 60 } 61 62 // Set the minute value 63 Time &Time::setMinute( int m ) 64 { 65 minute = ( m >= 0 && m < 60 ) ? m : 0; 66 67 return *this; // enables cascading 68 } 69 70 // Set the second value 71 Time &Time::setSecond( int s ) 72 { 73 second = ( s >= 0 && s < 60 ) ? s : 0; 74 75 return *this; // enables cascading 76 } 77 78 // Get the hour value 79 int Time::getHour() const { return hour; } 80 81 // Get the minute value 82 int Time::getMinute() const { return minute; } 83 84 // Get the second value 85 int Time::getSecond() const { return second; } 86 87 // Display military format time: HH:MM:SS 88 void Time::printMilitary() const 89 { 90 cout << ( hour < 10 ? "0" : "" ) << hour << ":" 91 << ( minute < 10 ? "0" : "" ) << minute; 92 } 93 94 // Display standard format time: HH:MM:SS AM (or PM) 95 void Time::printStandard() const 96 { 97 cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) 98 << ":" << ( minute < 10 ? "0" : "" ) << minute 99 << ":" << ( second < 10 ? "0" : "" ) << second 100 << ( hour < 12 ? " AM" : " PM" ); 101 } 102 // Fig. 7.8: fig07_08.cpp 103 // Cascading member function calls together 104 // with the this pointer 105 #include 106 #include "time6.h" 107 108 int main() 109 { 110 Time t; 111 112 t.setHour( 18 ).setMinute( 30 ).setSecond( 22 ); 113 cout << "Military time: "; 114 t.printMilitary(); 115 cout << "\nStandard time: "; 116 t.printStandard(); 117 118 cout << "\n\nNew standard time: "; 119 t.setTime( 20, 20, 20 ).printStandard(); 120 cout << endl; 121 122 return 0; 123 }